package com.meilai.project.controller.web.business.media;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.dto.media.ListMediaSpecDTO;
import com.meilai.project.entity.media.MediaTypeSpec;
import com.meilai.project.entity.media.MediaTypeSpecExt;
import com.meilai.project.service.business.media.MediaPointService;
import com.meilai.project.service.business.media.MediaTypeSpecService;
import com.meilai.project.vo.business.media.MediaTypeSpecVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "媒体类型规格")
@RestController
@RequestMapping("api/web/media/type-spec")
public class WebMediaTypeSpecController {

    @Autowired
    private MediaTypeSpecService mediaTypeSpecService;

    @Autowired
    private MediaPointService mediaPointService;

//    @GetMapping("/media_type_list")
//    @ApiOperation("获取媒体类型Select 会返回是否禁用和相关配置信息")
//    public ApiResult<List<MediaTypeSelectVO>> getAllMediaTypeSpecList() {
//        return ApiResult.ok(mediaTypeSpecService.getAllMediaTypeSpecList());
//    }

    @PostMapping("/list")
    @ApiOperation("列表")
    public ApiResult<Page<MediaTypeSpecExt>> list(
            @ApiParam("json") @RequestBody ListMediaSpecDTO req
    ) {
        return ApiResult.ok(mediaTypeSpecService.listByCondition(req));
    }

    @GetMapping("/list-by-media-type-id")
    @ApiOperation("列表(根据mediaTypeId查询)")
    public ApiResult<Page<MediaTypeSpecExt>> listByMediaTypeId(
            @ApiParam("mediaTypeId") Long mediaTypeId
    ) {
        return ApiResult.ok(mediaTypeSpecService.listByMediaTypeId(mediaTypeId));
    }

    @GetMapping("/list-type-spec-by-media-type-id")
    @ApiOperation("规格列表(根据mediaTypeId查询)")
    public ApiResult<List<String>> listTypeSpecByMediaTypeId(
            @ApiParam("mediaTypeId") Long mediaTypeId
    ) {
        return ApiResult.ok(mediaTypeSpecService.listTypeSpecByMediaTypeId(mediaTypeId));
    }

    @PostMapping("delete-by-id")
    @ApiOperation("删除（byId）")
    public ApiResult<Boolean> deleteById(@ApiParam("id") Long id) {
        boolean hasMediaPoint = mediaPointService.hasMediaPoint(id);
        if (hasMediaPoint) {
            return ApiResult.err("规格已关联点位，不可删除");
        }
        boolean result = mediaTypeSpecService.removeById(id);
        return ApiResult.ok(result);
    }

    @PostMapping("/save-or-update")
    @ApiOperation("保存/更新")
    public ApiResult<MediaTypeSpec> saveOrUpdate(@RequestBody @ApiParam("json") MediaTypeSpec mediaTypeSpec) {
        if (null == mediaTypeSpec.getId()) {
            mediaTypeSpecService.save(mediaTypeSpec);
        } else {
            mediaTypeSpecService.updateById(mediaTypeSpec);
        }
        return ApiResult.ok(mediaTypeSpec);
    }

    @GetMapping("/allList")
    @ApiOperation("获取所有规格列表")
    public ApiResult<List<MediaTypeSpecVO>> allList() {
        return ApiResult.ok(mediaTypeSpecService.allList());
    }
}
